package com.hyc.myvideo.ui.fragment.home;

import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.ClassCommentAdapter;
import com.hyc.myvideo.data.api.constants.Constants;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.Comment;
import com.hyc.myvideo.ui.activity.my.LoginActivity;
import com.hyc.myvideo.ui.base.BaseFragment;
import com.hyc.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 评论
 * Created by dfeads on 2017/7/22.
 */

public class CommentFragment extends BaseFragment {
  @BindView(R.id.fragment_comment_pull_refresh_list_view) PullToRefreshListView
      mPullRefreshListView;
  /** 发送 */
  @BindView(R.id.fragment_comment_send) Button mSend;
  /** 评论内容 */
  @BindView(R.id.fragment_comment_text) EditText mCommentText;

  private String curriculum_hour_id = "";
  private int page = 1;

  private ClassCommentAdapter adapter;
  private List<Comment> commentList;

  private boolean isRefresh = true;

  @Override protected int getLayoutId() {
    return R.layout.fragment_comment;
  }

  @Override protected void initView() {

    commentList = new ArrayList<>();
    adapter = new ClassCommentAdapter(getContext(), commentList);
    mPullRefreshListView.setAdapter(adapter);

    if (!StringUtils.isEmpty(curriculum_hour_id)) {
      api.getHomeClassHourCommentList(curriculum_hour_id, page);
    }

    mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
    mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
      @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        isRefresh = true;
        page = 1;
        api.getHomeClassHourCommentList(curriculum_hour_id, page);
      }

      @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        isRefresh = false;
        page++;
        api.getHomeClassHourCommentList(curriculum_hour_id, page);
      }
    });
  }

  @Override protected void initData() {
  }

  @Override protected void onNexts(String result, String method) {
    //列表
    if (method.equals(Constants.API_CURRICULUM_HOUR_COMMENT_LIST)) {
      BaseResult<List<Comment>> commentB =
          JSONObject.parseObject(result, new TypeReference<BaseResult<List<Comment>>>() {
          });
      if (commentB.isCode()) {
        if (isRefresh){
          commentList.clear();
        }
        commentList.addAll(commentB.getResult());
        adapter.updateListView(commentList);
      } else {
        Toaster.show(commentB.getMsg());
      }
    } else if (method.equals(Constants.API_SET_CURRICULUM_HOUR)) {
      BaseResult<String> base =
          JSONObject.parseObject(result, new TypeReference<BaseResult<String>>() {
          });
      if (base.isCode()) {
        isRefresh=true;
        page=1;
        api.getHomeClassHourCommentList(curriculum_hour_id, page);
        mCommentText.setText("");
      }
      Toaster.show(base.getMsg());
    }
    if (mPullRefreshListView.isRefreshing()) {
      mPullRefreshListView.onRefreshComplete();
    }
  }

  @Override protected void onError() {
    if (mPullRefreshListView.isRefreshing()) {
      mPullRefreshListView.onRefreshComplete();
    }
  }

  @OnClick(R.id.fragment_comment_send) public void onViewClicked() {
    if (userInfo.isLogin()) {
      api.publishClassComment(userInfo.getUser_id(),curriculum_hour_id, mCommentText.getText().toString().trim());
    } else {
      startActivity(new Intent(getContext(), LoginActivity.class));
    }
  }

  public void setCurriculum_hour_id(String curriculum_hour_id) {
    this.curriculum_hour_id = curriculum_hour_id;
  }
}
